#include "SpawnDoor.h"
#include "../SGD Wrappers/SGD_Event.h"
#include "GameplayState.h"
#include "../SGD Wrappers/SGD_AudioManager.h"
#include "CreateDogMessage.h"
#include "CreateEliteGuardMessage.h"

SpawnDoor::SpawnDoor() 
{
	RegisterForEvent("PAUSE");
	RegisterForEvent("UNPAUSE");
	RegisterForEvent("ALARM_TRIGGER");
	RegisterForEvent("ALARM_OFF");
	

	alarmSound = &GameplayState::GetInstance()->alarmSound;
}


SpawnDoor::~SpawnDoor()
{
	alarmSound = nullptr;
}

/*virtual*/ void SpawnDoor::Update(float dt)	 /*override*/
{

	SGD::AudioManager* pAudio = SGD::AudioManager::GetInstance();
	
	if (isActive == true)
	{
		if (pAudio->IsAudioPlaying(*alarmSound) == false)
			voice = pAudio->PlayAudio(*alarmSound, true);
		pAudio->SetVoiceVolume(voice, 25);
		if (doneSpawning == false)
		{
			if (m_nNumEnemies < m_nMaxEnemies)
			{
				if (spawnTimer.Update(dt))
				{
					if (GetType() == OBJ_DOG_SPAWNER)
					{
						CreateDogMessage* dogMsg = new CreateDogMessage(this);
						dogMsg->QueueMessage();
						dogMsg = nullptr;

					}
					else if (GetType() == OBJ_GUARD_SPAWNER)
					{
						CreateEliteGuardMessage* guardMsg = new CreateEliteGuardMessage(this);

						guardMsg->QueueMessage();
						guardMsg = nullptr;

					}
					
					m_nNumEnemies += 1;
				}
			}

			else
				doneSpawning = true;
		}
		else
		{
			if (m_nNumEnemies == 0)
			{
				isActive = false;
				if (pAudio->IsAudioPlaying(*alarmSound))
					pAudio->StopAudio(*alarmSound);
			}
		}
		
		
		
	}
}
/*virtual*/ void SpawnDoor::Render()			 /*override*/
{

}
/*virtual*/ void SpawnDoor::HandleEvent(const SGD::Event* pEvent)
{
	if (pEvent->GetEventID() == "ALARM_TRIGGER")
	{
		if (isActive == false)
		{
			isActive = true;
			doneSpawning = false;
			spawnTimer.AddTime(3);
		}
		

	
	}
	if (pEvent->GetEventID() == "ALARM_OFF")
	{
		isActive = false;
		doneSpawning = true;
		SGD::AudioManager::GetInstance()->StopAudio(*alarmSound);

	}
	if (pEvent->GetEventID() == "PAUSE")
	{
		if (SGD::AudioManager::GetInstance()->IsVoiceValid(voice))
			SGD::AudioManager::GetInstance()->PauseVoice(voice, true);

	}
	
	if (pEvent->GetEventID() == "UNPAUSE")
	{
		if (SGD::AudioManager::GetInstance()->IsVoiceValid(voice))
			SGD::AudioManager::GetInstance()->PauseVoice(voice, false);

	}

	
}
/*virtual*/ void SpawnDoor::HandleCollision(const IBase* pOther) /*override*/
{

}
void SpawnDoor::RemoveEnemy() 
{ 
	m_nNumEnemies--;
	if (m_nNumEnemies < 0)
		m_nNumEnemies = 0;
}
